#!/bin/bash
#
# An example hook script to block unannotated tags from entering.
# Called by "git receive-pack" with arguments: refname sha1-old sha1-new
#
# To enable this hook, rename this file to "update".
#
# Config
# ------
# hooks.allowunannotated
#   This boolean sets whether unannotated tags will be allowed into the
#   repository.  By default they won't be.
# hooks.allowdeletetag
#   This boolean sets whether deleting tags will be allowed in the
#   repository.  By default they won't be.
# hooks.allowmodifytag
#   This boolean sets whether a tag may be modified after creation. By default
#   it won't be.
# hooks.allowdeletebranch
#   This boolean sets whether deleting branches will be allowed in the
#   repository.  By default they won't be.
# hooks.denycreatebranch
#   This boolean sets whether remotely creating branches will be denied


refname="$1"
oldrev="$2"
newrev="$3"

echo "$refname"
echo "$oldrev"
echo "$newrev"

nginxHostPrefix="http://192.168.106.128/sonarlint"
nginxPathPrefix="/opt/gitlab/embedded/service/gitlab-rails/public/sonarlint"
changeFileCopyJar="/sonarlint/sonarlint-copy/jgit-learn-1.0-SNAPSHOT.jar"
sonarlintHome="/sonarlint/sonarlint-cli"
sonarlintJar="$sonarlintHome""/sonarlint-cli-2.0.jar"


sonarlint="/sonarlint"
nginxSrcPath="$nginxPathPrefix""$sonarlint""/""$newrev""/src"
nginxReportPath="$nginxPathPrefix""$sonarlint""/""$newrev""/report/index.html"
accessPath="$nginxHostPrefix""$sonarlint""/""$newrev""/report/index.html"

#echo $nginxSrcPath
#echo $nginxReportPath
#echo $accessPath


zero="0000000000000000000000000000000000000000"
if [ $oldrev = $zero -o $newrev = $zero ];then
	echo " can't to analysis 'create OR delete' branch "
	exit 0
fi


branchName=${refname##*/}
echo "$branchName" 

if [[ $branchName =~ dev_* || $branchName =~ fixbug_* ]]
then
	echo "$branchName need to analysis "
else
	echo "$branchName will not to aalysis , only 'dev_' OR 'fixbug_' to be ayalysis "
	exit 0
fi


gitDir=$(pwd)
echo $gitDir



changeFiles=$(git diff --name-only $oldrev $newrev | xargs )
echo $changeFiles


/sonarlint/jdk1.8.0_101/bin/java -jar $changeFileCopyJar $gitDir $newrev $nginxSrcPath $changeFiles


if [ ! -d "$nginxSrcPath" ]; then
	echo " no file to analysis "
	exit 0
else	
	echo "file copy over, analysis start"
fi


/sonarlint/jdk1.8.0_101/bin/java -jar -Dsonarlint.home=$sonarlintHome -Dproject.home=$nginxSrcPath $sonarlintJar --src **/*.java --html-report $nginxReportPath --charset UTF-8
echo "/sonarlint/jdk1.8.0_101/bin/java -jar -Dsonarlint.home=$sonarlintHome -Dproject.home=$nginxSrcPath $sonarlintJar --src **/*.java --html-report $nginxReportPath --charset UTF-8"

if [ -f "$nginxReportPath" ]; then
	reportHtml=$(cat $nginxReportPath)
	if [[ "$reportHtml" =~ "No issues" ]]
	then
		echo "no bug find,you are good"
	else
		echo "=========================================================================================================================="
		echo " your code have some bugs, please see : ""$accessPath"
		echo "=========================================================================================================================="
		exit 1
	fi
fi

exit 0 



